import { onMounted, reactive } from 'vue';
import { activi, allInstance, hangup, shutDown } from '@api/url/processManagement';
import notification from '@arco-design/web-vue/es/notification';
import router from '@/router';

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      total: 0, //总条数
      current: 1,
      pageSize: 10,
    },
    tableList: null,
    visible: false,
    form: {
      reason: '',
    },
    currentId: null,
  });

  /**
   * @exports getTable 销售订单列表数据获取
   */
  const getTable = () => {
    const obj = {
      size: base.pagination.pageSize,
      current: base.pagination.current,
    };

    allInstance(obj).then((res: any) => {
      if (res.code == '200') {
        base.tableList = res.data.list;
        base.tableList.map((rev: any, index: any) => {
          rev.key = index;
        });
        base.pagination.total = res.data.total;
      }
    });
  };

  //切换挂起状态
  const changeStatus = (item) => {
    if (item.isSuspended) {
      hangup(item.processInstanceId).then((res) => {
        if (res.code == 200) {
          notification.info({
            content: res.data,
          });
        }
      });
    } else {
      activi(item.processInstanceId).then((res) => {
        notification.info({
          content: res.data,
        });
      });
    }
  };

  //撤销挂起
  const revokePending = (item) => {
    base.visible = true;
  };

  //确定取消原因
  const handleOk = () => {
    const obj = {
      processInstanceId: base.currentId,
      reason: base.form.reason,
    };
    shutDown(obj).then((res) => {
      if (res.code == 200) {
        notification.info({
          content: res.data,
        });
      }
    });
  };

  //查看详情
  const viewDetails = (item) => {
    if (item.processDefinitionId.indexOf('purtc') > -1) {
      router.push({
        path: '/purchaseOrderDetails',
        query: {
          type: item.TC001,
          id: item.TC002,
        },
      });
    } else if (item.processDefinitionId.indexOf('coptc') > -1) {
      router.push({
        path: '/purchaseOrderDetails',
        query: {
          db: item.TC001,
          dh: item.TC002,
        },
      });
    }
  };

  onMounted(() => {
    getTable();
  });

  return {
    base,
    getTable,
    changeStatus,
    handleOk,
    revokePending,
    viewDetails,
  };
}
